package com.uit.se6.customui.android.navigation.SideDrawer;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * required API level 1
 * 
 * @author chx93
 * 
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class SideDrawerActivity2 extends Activity {
	protected DrawerLayout mDrawerLayout;
	protected ListView mDrawerList;
	protected ActionBarDrawerToggle mDrawerToggle;

	// nav drawer title
	protected CharSequence mDrawerTitle;
	protected CharSequence mTitle;

	protected SideDrawerListAdapter adapter;

	private SideDrawerListener listener;
	private String[] navMenuTitles;

	private ArrayList<SideDrawerItem> sideDrawerItems;
	private int fragmentId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTitle = mDrawerTitle = getTitle();
	}

	public void initializeLayout(DrawerLayout mDrawerLayout,
			ListView mDrawerList, int fragmentLayoutId) {
		this.mDrawerLayout = mDrawerLayout;
		this.mDrawerList = mDrawerList;
		this.fragmentId = fragmentLayoutId;
		mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				displayView(position);
			}
		});
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// initialize other attributes
		this.mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}

	public void initializeMenu(int drawerIcon, int drawerLabel,
			ArrayList<SideDrawerItem> sideDrawerItems) {
		if (mDrawerLayout == null || mDrawerList == null || fragmentId == 0) {
			Log.e(this.getClass().getName(), "Please initialize layout first!");
			return;
		}

		this.sideDrawerItems = sideDrawerItems;
		resetListTitle();

		// adapter
		adapter = new SideDrawerListAdapter(getApplicationContext(),
				sideDrawerItems);

		mDrawerList.setAdapter(adapter);

		// toggle
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				drawerIcon, drawerLabel, drawerLabel) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(mTitle);
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(mDrawerTitle);
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);
	}

	public void notifySideDrawerItem(ArrayList<SideDrawerItem> sideDrawerItems) {
		this.sideDrawerItems = sideDrawerItems;
		resetListTitle();
	}

	public void setupContainer(SideDrawerListener l) {
		this.listener = l;
	}

	private void resetListTitle() {
		int size = sideDrawerItems.size();
		navMenuTitles = new String[size];
		for (int i = 0; i < size; i++) {
			navMenuTitles[i] = sideDrawerItems.get(i).getTitle();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	protected void displayView(int position) {
		if (listener == null) {
			Log.e("Side Drawer", "You haven't set SideDrawer listener!");
		}

		// update the main content by replacing fragments
		Fragment fragment = listener.getSelectedFragment(position);

		if (fragment != null) {
			FragmentManager fragmentManager = listener.getActivity()
					.getFragmentManager();
			fragmentManager.beginTransaction().replace(fragmentId, fragment)
					.commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			mDrawerList.setSelection(position);
			setTitle(navMenuTitles[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
		} else {
			Log.e(this.getClass().getName(), "Error in creating fragment");
		}
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	public interface SideDrawerListener {
		Fragment getSelectedFragment(int position);

		Activity getActivity();
	}
}
